package kael.aiur.util.string;

import java.util.HashMap;
import java.util.Map;

import kael.aiur.exception.ParameterFormatException;

public class SymmetricalSymbol {
	private static final Map<String,String> symmetry = 
			new HashMap<String, String>();
	static {
		symmetry.put("(", ")");
		symmetry.put("[", "]");
		symmetry.put("{", "}");
		symmetry.put("<", ">");
		symmetry.put("【", "】");
		symmetry.put("（", "）");
		symmetry.put(")", "(");
		symmetry.put("]", "[");
		symmetry.put("}", "{");
		symmetry.put(">", "<");
		symmetry.put("】", "【");
		symmetry.put("）", "（");
	}
	/**
	 * 判断参数包裹符号pre和suff是否对称，包裹符号是指()/{}/[]等
	 * @param pre
	 * @param suff
	 * @return
	 */
	public static boolean isWrapSymmetry(String pre,String suff){
		String symm = symmetry.get(pre);
		if(symm==null){
			return false;
		}
		return symm.equals(suff);
	}
	/**
	 * 传入一个包裹符号的一半，返回该包裹符号的另一半，比如传入\'(\'会返回\')\'，
	 * 如果传入的不是包裹符号的一边，则抛出参数格式错误的异常。
	 * @param symbol
	 * @return
	 * @throws ParameterFormatException 如果传入的不是包裹符号则抛出的异常
	 */
	public static String getWarpSymmetry(String symbol) {
		if(!symmetry.keySet().contains(symbol)){
			return null;
		}
		return symmetry.get(symbol);
	}
	
	public static boolean isWrapCouple(String wrapCouple){
		if(wrapCouple.length() > 2){
			return false;
		}
		String[] wraps = wrapCouple.split("");
		return symmetry.get(wraps[1]).equals(wraps[2]);
	}
}
